<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;

//需要记录日志抛出这个异常
class InternalException extends Exception
{
    protected $msgForUser;

    public function __construct(string $message, string $msgForUser = '系统内部错误', int $code = 500, int $errorCode = 500, $data = [])
    {
        parent::__construct($message, $code);
        $this->msgForUser = $msgForUser;
        $this->errorCode = $errorCode;
        $this->data = $data;
    }

    public function render(Request $request)
    {
        if ($request->expectsJson()) {
            return response()->json(['code' => $this->errorCode, 'message' => $this->msgForUser, 'data' => $this->data], $this->code);
        }

        return view('pages.error', ['msg' => $this->msgForUser]);
    }
}